﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Template;

namespace DynaCAD
{
    /// <summary>
    /// Interaction logic for PartCreator.xaml
    /// </summary>
    public partial class PartCreator : Window
    {
        AdornerLayer ADL;
        public pvdcd.Part Part;
        public List<Template.ReferanceTemplate> Templates;
        public Template.ReferanceTemplate SelectedTemplate;
        public Template.Type SelectedType;
        public Point? RectangelePoint = null;
        public Point? MidPointPosition = null;
        public Path SelectedTemplateEllipse = null;
        public Path SelectedPartEllipse = null;
        public List<UIElement> SelectedUIElements = new List<UIElement>();
        public List<UIElement> CopiedUIElements = new List<UIElement>();
        public string Aim = "";
        public Point? FirstPoint;
        public List<PathMaker.Node> SeamNodes;
        public LineWithArrows.LineArrow RemoveLineArrow;
        public Path RemoveSeam;
        public PartCreator()
        {
            this.RemoveSeam = new Path();
            this.SeamNodes = new List<PathMaker.Node>();
            this.RemoveLineArrow = new LineWithArrows.LineArrow();
            this.FirstPoint = null;
            this.Part = new pvdcd.Part();
            this.Templates = new List<Template.ReferanceTemplate>();
            InitializeComponent();
            this.FrontLeftGrid.Children.Add((Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("GridLineCanvas")));
            this.FrontRightGrid.Children.Add((Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("GridLineCanvas")));
            this.BackLeftGrid.Children.Add((Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("GridLineCanvas")));
            this.BackRightGrid.Children.Add((Canvas)XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("GridLineCanvas")));
            TemplateComboBox.SelectionChanged+=new SelectionChangedEventHandler(TemplateComboBoxSelected);
            PartTypeComboBox.SelectionChanged+=new SelectionChangedEventHandler(PartTypeComboBoxSelected);
            LoadTemplates();
            LoadTemplateComboBox();
        }
        private void LoadTemplates()
        {            
                this.Templates = Karbel.Data.GetSablonlar(); 
        }
        private void LoadTemplateComboBox()
        {
            TemplateComboBox.Items.Clear();
            foreach (ReferanceTemplate Tmp in Templates)
            {
                TextBlock T = new TextBlock();
                T.Text = Tmp.Name;
                TemplateComboBox.Items.Add(T);
            }
            try
            {
                TemplateComboBox.SelectedItem = TemplateComboBox.Items[0];
                return;
            }
            catch { MessageBox.Show("Kayıtlı şablon yok!, Lütfen ana sayfadan şablon oluşturun.");
                return; 
            }
        }
        private void LoadPartTypeComboBox()
        {
            PartTypeComboBox.Items.Clear();
            if(SelectedTemplate!=null)
            foreach (Template.Type PT in SelectedTemplate.PartTypeList)
            {
                TextBlock T = new TextBlock();
                T.Text = PT.PartType;
                PartTypeComboBox.Items.Add(T);
            }
        }
        private void FillTemplatePoints()
        {
            TemplatePointCanvas.Children.Clear();
            foreach (PartPoint P in SelectedType.FrontLeftPoints)
            {
                P.Aktive = false;
                P.Ellipse.IsMouseDirectlyOverChanged+=new DependencyPropertyChangedEventHandler(MouseDirectlyOverChangedOnEllipse);
                P.Ellipse.MouseLeftButtonDown +=new MouseButtonEventHandler(MouseLeftButtonDownOnEllipse);
                TemplatePointCanvas.Children.Add(P.Ellipse);
            }
            foreach (PartPoint P in SelectedType.FrontRightPoints)
            {
                P.Aktive = false;
                P.Ellipse.IsMouseDirectlyOverChanged += new DependencyPropertyChangedEventHandler(MouseDirectlyOverChangedOnEllipse);
                P.Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDownOnEllipse);
                TemplatePointCanvas.Children.Add(P.Ellipse);
            }
            foreach (PartPoint P in SelectedType.BackLeftPoints)
            {
                P.Aktive = false;
                P.Ellipse.IsMouseDirectlyOverChanged += new DependencyPropertyChangedEventHandler(MouseDirectlyOverChangedOnEllipse);
                P.Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDownOnEllipse);
                TemplatePointCanvas.Children.Add(P.Ellipse);
            }
            foreach (PartPoint P in SelectedType.BackRightPoints)
            {
                P.Aktive = false;
                P.Ellipse.IsMouseDirectlyOverChanged += new DependencyPropertyChangedEventHandler(MouseDirectlyOverChangedOnEllipse);
                P.Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDownOnEllipse);
                TemplatePointCanvas.Children.Add(P.Ellipse);
            }
        }
        private void UnSelectTemplateEllipse()
        {
            if (this.SelectedTemplateEllipse != null)
            {
                SelectedType.UpdateType();
                ((EllipseGeometry)(SelectedTemplateEllipse.Data)).RadiusX = 5;
                ((EllipseGeometry)(SelectedTemplateEllipse.Data)).RadiusY = 5;
                this.SelectedTemplateEllipse = null;
            }
        } 

        //Events//
        private void TabControlSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectedTemplate != null)
            {
                int i = TABS.SelectedIndex;
                if (i == 0 || i == 1)
                {
                    TemplateModelCanvas.Children.Clear();
                    TemplateModelCanvas.Children.Add(SelectedTemplate.ModelOn);
                    FillTemplatePoints();
                    if (i == 0)
                    {
                        foreach (PartPoint P in SelectedType.FrontLeftPoints)
                            P.Aktive = true;
                    }
                    else
                    {
                        foreach (PartPoint P in SelectedType.FrontRightPoints)
                            P.Aktive = true;
                    }
                }
                else if (i == 2 || i == 3)
                {
                    TemplateModelCanvas.Children.Clear();
                    TemplateModelCanvas.Children.Add(SelectedTemplate.ModelArka);
                    FillTemplatePoints();
                    if (i == 2)
                    {
                        foreach (PartPoint P in SelectedType.BackLeftPoints)
                            P.Aktive = true;
                    }
                    else
                    {
                        foreach (PartPoint P in SelectedType.BackRightPoints)
                            P.Aktive = true;
                    }
                }
                else
                {
                    FillTemplatePoints();
                }
                EscapeKey();
            }
        }
        private void TemplateComboBoxSelected(object sender, SelectionChangedEventArgs e)
        {
            TextBlock TB =  ((TextBlock)(sender as ComboBox).SelectedItem);
            foreach (ReferanceTemplate T in Templates)
            {
                if (T.Name == TB.Text) 
                {
                    SelectedTemplate = T;
                    break;
                }
            }
            LoadPartTypeComboBox();
            PartTypeComboBox.SelectedItem = PartTypeComboBox.Items[0];
            TABS.SelectedItem = FrontRightTab;
            TABS.SelectedItem = FrontLeftTab;
            EscapeKey();
        }
        private void PartTypeComboBoxSelected(object sender, SelectionChangedEventArgs e)
        {
                TextBlock TB = ((TextBlock)(sender as ComboBox).SelectedItem);
                if (TB == null)
                    return;
                foreach (Template.Type T in SelectedTemplate.PartTypeList)
                {
                    if (T.PartType == TB.Text)
                    {
                        SelectedType = T;
                        break;
                    }
                }
                if (!SelectedType.FrontBack&& !SelectedType.LeftRight)
                {
                    FrontLeftTab.Visibility = Visibility.Visible;
                    FrontRightTab.Visibility = Visibility.Collapsed;
                    BackLeftTab.Visibility = Visibility.Collapsed;
                    BackRightTab.Visibility = Visibility.Collapsed;

                    FrontLeftTab.Header = "Ön";
                }
                else if (SelectedType.FrontBack && !SelectedType.LeftRight)
                {
                    FrontLeftTab.Visibility = Visibility.Visible;
                    FrontRightTab.Visibility = Visibility.Collapsed;
                    BackLeftTab.Visibility = Visibility.Visible;
                    BackRightTab.Visibility = Visibility.Collapsed;

                    FrontLeftTab.Header = "Ön";
                    BackLeftTab.Header = "Arka";
                }
                else if (!SelectedType.FrontBack && SelectedType.LeftRight)
                {
                    FrontLeftTab.Visibility = Visibility.Visible;
                    FrontRightTab.Visibility = Visibility.Visible;
                    BackLeftTab.Visibility = Visibility.Collapsed;
                    BackRightTab.Visibility = Visibility.Collapsed;

                    FrontLeftTab.Header = "Ön-Sol";
                    FrontRightTab.Header = "Ön-Sağ";
                }
                else if (SelectedType.FrontBack && SelectedType.LeftRight)
                {
                    FrontLeftTab.Visibility = Visibility.Visible;
                    FrontRightTab.Visibility = Visibility.Visible;
                    BackLeftTab.Visibility = Visibility.Visible;
                    BackRightTab.Visibility = Visibility.Visible;

                    FrontLeftTab.Header = "Ön-Sol";
                    FrontRightTab.Header = "Ön-Sağ";
                    BackLeftTab.Header = "Arka-Sol";
                    BackRightTab.Header = "Arka-Sağ";
                }

                if (SelectedType.PartType == "Yaka")
                {
                    Detay0.Visibility = Visibility.Visible;
                    Detay1.Visibility = Visibility.Collapsed;
                    Detay2.Visibility = Visibility.Collapsed;
                    Detay0.Header = "Detay (Yaka Açık Görünüş)";
                }
                else if (SelectedType.PartType == "Manşet")
                {
                    Detay0.Visibility = Visibility.Visible;
                    Detay1.Visibility = Visibility.Collapsed;
                    Detay2.Visibility = Visibility.Collapsed;
                    Detay0.Header = "Detay (Manşet Açık Görünüş)";
                }
                else if (SelectedType.PartType == "Pat")
                {
                    Detay0.Visibility = Visibility.Visible;
                    Detay1.Visibility = Visibility.Visible;
                    Detay2.Visibility = Visibility.Visible;
                    Detay0.Header = "Detay (Pat İlik ve Cep Ölçüleri Yeri)";
                    Detay1.Header = "Detay (Pat İçten Görünüm)";
                    Detay2.Header = "Detay (Pat Katlaması)";
                }
                else if (SelectedType.PartType == "Sev Böcek")
                {
                    Detay0.Visibility = Visibility.Visible;
                    Detay1.Visibility = Visibility.Collapsed;
                    Detay2.Visibility = Visibility.Collapsed;
                    Detay0.Header = "Detay (Sev Böcek İçten ve Dıştan Görünüm)";
                }
                else
                {
                    Detay0.Visibility = Visibility.Collapsed;
                    Detay1.Visibility = Visibility.Collapsed;
                    Detay2.Visibility = Visibility.Collapsed;
                }
                TABS.SelectedItem = FrontRightTab;
                TABS.SelectedItem = FrontLeftTab;
                EscapeKey();
        }
        private void MouseDirectlyOverChangedOnEllipse(object sender, DependencyPropertyChangedEventArgs e)
        {
            Path P=sender as Path;
            if (P.IsMouseOver)
            {
                ((EllipseGeometry)(P.Data)).RadiusX = 10;
                ((EllipseGeometry)(P.Data)).RadiusY = 10;
            }
            else if(P!=SelectedTemplateEllipse)
            {
                ((EllipseGeometry)(P.Data)).RadiusX = 5;
                ((EllipseGeometry)(P.Data)).RadiusY = 5;
            }
        }
        private void MouseLeftButtonDownOnEllipse(object sender, MouseButtonEventArgs e)
        {
            Path P=sender as Path;
            if (SelectedTemplateEllipse != null)
            {
                SelectedType.UpdateType();
                ((EllipseGeometry)(SelectedTemplateEllipse.Data)).RadiusX = 5;
                ((EllipseGeometry)(SelectedTemplateEllipse.Data)).RadiusY = 5;
            }
            SelectedTemplateEllipse = P;
            ((EllipseGeometry)(SelectedTemplateEllipse.Data)).RadiusX = 10;
            ((EllipseGeometry)(SelectedTemplateEllipse.Data)).RadiusY = 10;
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            foreach (Window a in Application.Current.Windows)
            {
                try
                {
                    a.Visibility = Visibility.Visible;
                }
                catch { }
            }
        }
        private List<double> GetCanvasDimensions(Canvas C)
        {
            double W,H,X,Y;
            W = H = -5000;
            X = Y = 5000;
            List<double> d = new List<double>();
            if (C.Children.Count < 0)
            {
                return d;
            }
            else if(!(C.Children[0] is Canvas))
            {
                foreach (UIElement p in C.Children.OfType<UIElement>().ToList<UIElement>())
                {
                    if (p is Path)
                    {
                        if (p.TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds).BottomRight.X > W)
                            W = p.TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds).BottomRight.X;
                        if (p.TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds).BottomRight.Y > H)
                            H = p.TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds).BottomRight.Y;
                        if (p.TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds).TopLeft.X < X)
                            X = p.TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds).TopLeft.X;
                        if (p.TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds).TopLeft.Y < Y)
                            Y = p.TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds).TopLeft.Y;
                    }
                    else if (p is LineWithArrows.LineArrow)
                    {
                        Point TopLeft = new Point(Math.Min(((LineWithArrows.LineArrow)p).StartPoint.X, ((LineWithArrows.LineArrow)p).EndPoint.X),
                                                  Math.Min(((LineWithArrows.LineArrow)p).StartPoint.Y, ((LineWithArrows.LineArrow)p).EndPoint.Y));
                        Point BottomRight = new Point(Math.Max(((LineWithArrows.LineArrow)p).StartPoint.X, ((LineWithArrows.LineArrow)p).EndPoint.X),
                                                  Math.Max(((LineWithArrows.LineArrow)p).StartPoint.Y, ((LineWithArrows.LineArrow)p).EndPoint.Y));
                        if (BottomRight.X > W)
                            W = BottomRight.X;
                        if (BottomRight.Y > H)
                            H = BottomRight.Y;
                        if (TopLeft.X < X)
                            X = TopLeft.X;
                        if (TopLeft.Y < Y)
                            Y = TopLeft.Y;
                    }
                }
                if (W > X && H > Y)
                {
                    d.Add(W);
                    d.Add(H);
                    d.Add(X);
                    d.Add(Y);
                }
            }
            return d;
        }
        private Canvas PreparedCanvas(Canvas C)
        {
           Canvas Prepared=(Canvas)XamlEditor.CloneUIElement.cloneElement(C);
           List<double> d = GetCanvasDimensions(C);
           if (d.Count > 0)
           {
               Prepared.Width = d[0] - d[2];
               Prepared.Height = d[1] - d[3];
               foreach (UIElement UI in Prepared.Children.OfType<UIElement>().ToList<UIElement>())
               {
                   if (UI is LineWithArrows.LineArrow)
                   {
                       ((LineWithArrows.LineArrow)UI).StartPoint = new Point(((LineWithArrows.LineArrow)UI).StartPoint.X - d[2],
                                                                             ((LineWithArrows.LineArrow)UI).StartPoint.Y-d[3] );
                       ((LineWithArrows.LineArrow)UI).EndPoint = new Point(((LineWithArrows.LineArrow)UI).EndPoint.X - d[2],
                                                                             ((LineWithArrows.LineArrow)UI).EndPoint.Y - d[3]);
                   }
                   else
                   {
                       if (Double.IsNaN(Canvas.GetLeft(UI)))
                       {
                           Canvas.SetLeft(UI, -d[2]);
                       }
                       else
                       {
                           Canvas.SetLeft(UI, Canvas.GetLeft(UI) - d[2]);
                       }
                       if (Double.IsNaN(Canvas.GetTop(UI)))
                       {
                           Canvas.SetTop(UI, -d[3]);
                       }
                       else
                       {
                           Canvas.SetTop(UI, Canvas.GetTop(UI) - d[3]);
                       }
                   }
               }
           }
           return Prepared;
        }

        //MouseEvents//
        private void TemplateStackPanelMouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.MiddleButton == MouseButtonState.Pressed)
            {
                if (MidPointPosition != null)
                {
                    Point p = e.GetPosition(TemplateGrid);
                    XO1.TransformationOperations.MoveUIElement(TemplateStackPanel, p.X - MidPointPosition.Value.X, p.Y - MidPointPosition.Value.Y);
                    MidPointPosition = p;
                }
                else
                {
                    MidPointPosition = e.GetPosition(TemplateGrid);
                }
            }
            else
            {
                MidPointPosition = e.GetPosition(TemplateGrid);
            }
        }
        private void TemplateStackPanelMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(TemplateStackPanel, e.Delta > 0, e.GetPosition(TemplateStackPanel));
        }
        private void TemplateStackPanelMouseLeave(object sender, MouseEventArgs e)
        {
            MidPointPosition = null;
        }
        private void FillPartPoints()
        {
            FrontLeftPoints.Children.Clear();
            foreach (PartPoint P in Part.FrontLeftPoints)
                FrontLeftPoints.Children.Add(P.Ellipse);

            FrontRightPoints.Children.Clear();
            foreach (PartPoint P in Part.FrontRightPoints)
                FrontRightPoints.Children.Add(P.Ellipse);

            BackLeftPoints.Children.Clear();
            foreach (PartPoint P in Part.BackLeftPoints)
                BackLeftPoints.Children.Add(P.Ellipse);

            BackRightPoints.Children.Clear();
            foreach (PartPoint P in Part.BackRightPoints)
                BackRightPoints.Children.Add(P.Ellipse);
                
        }
        private bool StacksPanelMouseLeftButtonsDown(object sender, MouseButtonEventArgs e, List<PartPoint> PL, StackPanel S)
        {
            if (SelectedTemplateEllipse != null)
            {
                int Number;
                if (SelectedTemplateEllipse.Fill == Brushes.Blue)
                    Number = 1;
                else if (SelectedTemplateEllipse.Fill == Brushes.Red)
                    Number = 2;
                else if (SelectedTemplateEllipse.Fill == Brushes.Green)
                    Number = 3;
                else if (SelectedTemplateEllipse.Fill == Brushes.Yellow)
                    Number = 4;
                else
                    return true;
                Template.PartPoint P = PL.Find(p => p.PointNumber == Number);
                if (P == null)
                {
                    P = new Template.PartPoint(e.GetPosition(S).X, e.GetPosition(S).Y, Number);
                    PL.Add(P);
                    if (PL.Count > 2)
                        PL.Remove(PL[0]);
                }
                else
                {
                    P.X = e.GetPosition(S).X;
                    P.Y = e.GetPosition(S).Y;
                }
                FillPartPoints();
                UnSelectTemplateEllipse();
                return true;
            }
            else if (e.Source is Path)
            {
                if (((Path)(e.Source)).Data is EllipseGeometry)
                {
                    SelectedPartEllipse = e.Source as Path;
                    return true;
                }
                else
                {
                    RectangelePoint = e.GetPosition(S);
                    return false;
                }
            }
            else
            {
                RectangelePoint = e.GetPosition(S);
                return false;
            }
        
        }
        private void StacksPanelMouseMove(object sender, MouseEventArgs e, StackPanel S, Grid G, Rectangle R)
        {
            System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("/Images/CrossCursor.cur", UriKind.Relative));
            this.Cursor = new System.Windows.Input.Cursor(info.Stream); 
            if (Mouse.MiddleButton == MouseButtonState.Pressed)
            {
                if (MidPointPosition != null)
                {
                    Point p = e.GetPosition(G);
                    XO1.TransformationOperations.MoveUIElement(S, p.X - MidPointPosition.Value.X, p.Y - MidPointPosition.Value.Y);
                    MidPointPosition = p;
                }
                else
                {
                    MidPointPosition = e.GetPosition(G);
                }
            }
            else
            {
                MidPointPosition = e.GetPosition(G);
                if (Mouse.LeftButton == MouseButtonState.Pressed && SelectedPartEllipse != null)
                {
                    ((EllipseGeometry)(SelectedPartEllipse.Data)).Center = new Point(e.GetPosition(S).X, e.GetPosition(S).Y);
                    this.Part.PartUpDate();
                }
                else if (Mouse.LeftButton == MouseButtonState.Pressed && RectangelePoint!=null&&Aim=="")
                {
                    R.Visibility = Visibility.Visible;
                    Point p = e.GetPosition(S);
                    R.Width = Math.Abs(p.X - RectangelePoint.Value.X);
                    R.Height = Math.Abs(p.Y - RectangelePoint.Value.Y);
                    Canvas.SetLeft(R, Math.Min(p.X, RectangelePoint.Value.X));
                    Canvas.SetTop(R, Math.Min(p.Y, RectangelePoint.Value.Y));

                    Rect Rec = new Rect(new Point(Canvas.GetLeft(R),Canvas.GetTop(R)),
                                        new Point(Canvas.GetLeft(R)+R.Width,Canvas.GetTop(R)+R.Height));

                    RemoveAdorners();
                    SelectedUIElements.Clear();
                    Canvas C = (Canvas)(S.Children[0]);
                    if (C.Children.Count < 1)
                        return;
                    else 
                    {
                        C = (Canvas)(C.Children[0]);
                        if (C.Children.Count < 1)
                            return;
                        else if (C.Children[0] is Canvas)
                            C = (Canvas)(C.Children[0]);
                        if (C.Children.Count > 0)
                        {
                            if (C.Children[0] is Canvas)
                                C = (Canvas)(C.Children[0]);
                        }
                        if (C.Children.Count > 0)
                        {
                            if (C.Children[0] is Canvas)
                                C = (Canvas)(C.Children[0]);
                        }

                        C.Children.OfType<UIElement>().ToList<UIElement>().ForEach(UIElement=>
                            {
                                if (UIElement is Path)
                                {
                                    if (Rec.Contains(((Path)UIElement).TransformToVisual(S).TransformBounds(((Path)(UIElement)).Data.Bounds)))
                                    {
                                        UIElement.ClearValue(Path.NameProperty);
                                        SelectedUIElements.Add(UIElement);
                                    }
                                }
                                else if (UIElement is LineWithArrows.LineArrow)
                                {
                                    if (Rec.Contains(new Rect(((LineWithArrows.LineArrow)UIElement).StartPoint, ((LineWithArrows.LineArrow)UIElement).EndPoint)))
                                    {
                                        SelectedUIElements.Add(UIElement);
                                    }
                                }
                                else if (UIElement is TextBox)
                                {
                                    if (Rec.Contains(new Rect(new Point(Canvas.GetLeft((TextBox)UIElement), Canvas.GetTop((TextBox)UIElement)), new Point(Canvas.GetLeft((TextBox)UIElement)+5, Canvas.GetTop((TextBox)UIElement)+5))))
                                    {
                                        SelectedUIElements.Add(UIElement);
                                    }
                                }
                            });
                    }
                    AddAdorners();
                }
            }
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void NoteRemove(object sender, RoutedEventArgs e)
        {
            MenuItem MI = sender as MenuItem;
            UIElement UI = (UIElement)(((ContextMenu)(MI.Parent)).PlacementTarget);
            ((Canvas)(((TextBox)UI).Parent)).Children.Remove(UI);
            EscapeKey();
        }
        private void TextBoxMouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            RemoveAdorners();
            RectangelePoint = null;
            if (Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                TextBox T = sender as TextBox;
                EscapeKey();
                SelectedUIElements.Add(T);
            }
            else
            {
                SelectedUIElements.Clear();
                TextBox T = sender as TextBox;
                SelectedUIElements.Add(T);
            }
            AddAdorners();
        }
        private void RemoveAdorners()
        {
            try
            {
                foreach (UIElement p in SelectedUIElements)
                {
                    Adorner[] toRemoveArray = ADL.GetAdorners(p);

                    if (toRemoveArray != null)
                    {
                        for (int x = 0; x < toRemoveArray.Length; x++)
                        {
                            ADL.Remove(toRemoveArray[x]);
                        }
                    }
                }
            }
            catch { }
        }
        private void AddAdorners()
        {
            foreach (UIElement p in SelectedUIElements)
            {
                if (p is Path)
                {
                    ADL = AdornerLayer.GetAdornerLayer(p);
                    ADL.Add(new XamlEditor.ResizingAdorner(p));
                }
                else if (p is LineWithArrows.LineArrow)
                {
                    ADL = AdornerLayer.GetAdornerLayer(p);
                    ADL.Add(new XamlEditor.LineArrowAdorner((LineWithArrows.LineArrow)p));
                }
                else if (p is TextBox)
                {
                    ADL = AdornerLayer.GetAdornerLayer(p);
                    ADL.Add(new XamlEditor.TextBoxAdorner((TextBox)p));
                }
            }
        }
        private void GridMouseLeftButtonDown(StackPanel SP,Canvas C)
        {
            RectangelePoint = Mouse.GetPosition(SP);
            if (Aim == ""&&C.Children.Count>0)
            {
                Point ClickPoint = Mouse.GetPosition(C);
                Rect Rec = new Rect(new Point(ClickPoint.X - 5, ClickPoint.Y - 5), new Point(ClickPoint.X + 5, ClickPoint.Y + 5));
                ((Canvas)(C.Children[0])).Children.OfType<UIElement>().ToList<UIElement>().ForEach
                (p =>
                {
                    if (p is LineWithArrows.LineArrow)
                    {
                        Rect RecP = new Rect(((LineWithArrows.LineArrow)p).StartPoint, ((LineWithArrows.LineArrow)p).EndPoint);
                        if (RecP.IntersectsWith(Rec))
                        {
                            RemoveAdorners();
                            if (Keyboard.IsKeyDown(Key.LeftCtrl))
                            {
                                if (SelectedUIElements.Contains(p))
                                {
                                    SelectedUIElements.Remove(p);
                                    AddAdorners();
                                    return;
                                }
                                else
                                    SelectedUIElements.Add(p);
                            }
                            else
                            {
                                SelectedUIElements.Clear();
                                SelectedUIElements.Add(p);
                            }
                            AddAdorners();
                            return;
                        }
                    }
                    else if (p is Image)
                    {
                        double SX = 1;
                        double SY = 1;
                        if (p.RenderTransform is ScaleTransform)
                        {
                            SX = ((ScaleTransform)(p.RenderTransform)).ScaleX;
                            SY = ((ScaleTransform)(p.RenderTransform)).ScaleY;
                        }
                        if (((Image)p).Source != null)
                        {
                            Rect RecP = new Rect(new Point(Canvas.GetLeft(p), Canvas.GetTop(p)), new Point(((Image)p).Source.Width * SX + Canvas.GetLeft(p), ((Image)p).Source.Height * SY + Canvas.GetTop(p)));
                            if (RecP.IntersectsWith(Rec))
                            {
                                RemoveAdorners();
                                if (Keyboard.IsKeyDown(Key.LeftCtrl))
                                {
                                    if (SelectedUIElements.Contains(p))
                                    {
                                        SelectedUIElements.Remove(p);
                                        AddAdorners();
                                        return;
                                    }
                                    else
                                        SelectedUIElements.Add(p);
                                }
                                else
                                {
                                    SelectedUIElements.Clear();
                                    SelectedUIElements.Add(p);
                                }
                                AddAdorners();
                                return;
                            }
                        }
                    }
                    else if (p is Pats.Pat)
                    {
                        Rect RecP = new Rect(((Pats.Pat)p).StartPoint, ((Pats.Pat)p).EndPoint);
                        if (RecP.IntersectsWith(Rec))
                        {
                            RemoveAdorners();
                            if (Keyboard.IsKeyDown(Key.LeftCtrl))
                            {
                                if (SelectedUIElements.Contains(p))
                                {
                                    SelectedUIElements.Remove(p);
                                    AddAdorners();
                                    return;
                                }
                                else
                                    SelectedUIElements.Add(p);
                            }
                            else
                            {
                                SelectedUIElements.Clear();
                                SelectedUIElements.Add(p);
                            }
                            AddAdorners();
                            return;
                        }
                    }
                    else if (p is Path)
                    {
                        if (Rec.IntersectsWith(((Path)p).TransformToVisual(C).TransformBounds(((Path)p).Data.Bounds)))
                        {
                            RemoveAdorners();
                            if (Keyboard.IsKeyDown(Key.LeftCtrl))
                            {
                                if (SelectedUIElements.Contains(p))
                                {
                                    SelectedUIElements.Remove(p);
                                    AddAdorners();
                                    return;
                                }
                                else
                                    SelectedUIElements.Add(p);
                            }
                            else
                            {
                                SelectedUIElements.Clear();
                                SelectedUIElements.Add(p);
                            }
                            AddAdorners();
                            return;
                        }
                    }
                });
            }
            else if (Aim == "Note")
            {
                TextBox T = new TextBox();
                T.Background = Brushes.Transparent;
                T.Height = 20;
                T.Width = 80;
                Canvas.SetLeft(T, Mouse.GetPosition(SP).X);
                Canvas.SetTop(T, Mouse.GetPosition(SP).Y);
                T.Text = "Note";
                T.BorderBrush = Brushes.White;

                //
                MenuItem MenuItem1 = new MenuItem();
                MenuItem1.Header = "Notu Sil";
                MenuItem1.Click += new RoutedEventHandler(NoteRemove);
                MenuItem1.Icon = new System.Windows.Controls.Image
                {
                    Source = new BitmapImage(new Uri("Images/Delete.png", UriKind.Relative))
                };
                MenuItem1.Height = 20;
                T.ContextMenu = new ContextMenu();
                T.ContextMenu.Items.Add(MenuItem1);
                //
                T.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(TextBoxMouseLeftButtonDown);
                if (C.Children.Count > 0)
                {
                    ((Canvas)(C.Children[0])).Children.Add(T);
                }
                else
                {
                    Canvas New = new Canvas();
                    New.Children.Add(T);
                    C.Children.Add(New);
                }
                RemoveAdorners();
                SelectedUIElements.Add(T);
                AddAdorners();
                this.Cursor = Cursors.Arrow;
                Aim = "";
            }
            else if ((Aim == "Arrow" || Aim == "DoubleArrow" || Aim == "Pat") && FirstPoint == null)
            {
                FirstPoint = Mouse.GetPosition(C);
                return;
            }
            else if ((Aim == "Arrow" || Aim == "DoubleArrow") && FirstPoint != null)
            {
                if (Aim == "Arrow")
                {
                    LineWithArrows.LineArrow A = new LineWithArrows.LineArrow();
                    A.StartPoint = (Point)FirstPoint;
                    A.EndPoint = Mouse.GetPosition(C);
                    A.Caps = LineWithArrows.LineArrow.ArrowEnds.End;
                    A.Stroke = Brushes.Black;
                    if (C.Children.Count > 0)
                    {
                        ((Canvas)(C.Children[0])).Children.Add(A);
                    }
                    else
                    {
                        Canvas New = new Canvas();
                        New.Children.Add(A);
                        C.Children.Add(New);
                    }
                    try { ((Canvas)(RemoveLineArrow.Parent)).Children.Remove(RemoveLineArrow); }
                    catch { }
                }
                else if (Aim == "DoubleArrow")
                {
                    LineWithArrows.LineArrow A = new LineWithArrows.LineArrow();
                    A.StartPoint = (Point)FirstPoint;
                    A.EndPoint = Mouse.GetPosition(C);
                    A.Caps = LineWithArrows.LineArrow.ArrowEnds.Both;
                    A.Stroke = Brushes.Black;
                    if (C.Children.Count > 0)
                    {
                        ((Canvas)(C.Children[0])).Children.Add(A);
                    }
                    else
                    {
                        Canvas New = new Canvas();
                        New.Children.Add(A);
                        C.Children.Add(New);
                    }
                    try { ((Canvas)(RemoveLineArrow.Parent)).Children.Remove(RemoveLineArrow); }
                    catch { }
                }
                Aim = "";
                FirstPoint = null;
            }
            else if (Aim == "Seam" || Aim == "Spline")
            {
                SeamNodes.Add(new PathMaker.Node(NP: Mouse.GetPosition(C)));
            }
        }
        private void GridMouseLeftButtonUp(Canvas C)
        {
            RectangelePoint = null;
            FrontLeftRectangle.Visibility = Visibility.Collapsed;
            FrontRightRectangle.Visibility = Visibility.Collapsed;
            BackLeftRectangle.Visibility = Visibility.Collapsed;
            BackRightRectangle.Visibility = Visibility.Collapsed;
            DetayRectangle0.Visibility = Visibility.Collapsed;
            DetayRectangle1.Visibility = Visibility.Collapsed;
            DetayRectangle2.Visibility = Visibility.Collapsed;
            if ((Aim == "Seam" || Aim == "Spline") && SeamNodes.Count > 2)
            {
                Path S = new Path();
                S.Data = PathMaker.Node.GetPath(SeamNodes);
                if (Aim == "Seam")
                {
                    S.Stroke = Brushes.Red;
                    S.StrokeDashArray = new DoubleCollection() { 4, 2 };
                    S.StrokeThickness = 1;
                }
                else if (Aim == "Spline")
                {
                    S.Stroke = Brushes.Black;
                    S.StrokeThickness = 2;
                    S.ClearValue(Path.StrokeDashArrayProperty);
                }
                try { ((Canvas)(C.Children[0])).Children.Remove(RemoveSeam); }
                catch { }
                try
                {
                    ((Canvas)(C.Children[0])).Children.Remove(
                        ((Canvas)(C.Children[0])).Children.OfType<Path>().ToList<Path>().Last<Path>()
                                                                            );
                }
                catch { }
                ((Canvas)(C.Children[0])).Children.Add(S);
            }
            else if ((Aim == "Seam" || Aim == "Spline") && SeamNodes.Count > 1)
            {
                Path S = new Path();
                S.Data = PathMaker.Node.GetPath(SeamNodes);
                if (Aim == "Seam")
                {
                    S.Stroke = Brushes.Red;
                    S.StrokeDashArray = new DoubleCollection() { 4, 2 };
                    S.StrokeThickness = 1;
                }
                else if (Aim == "Spline")
                {
                    S.Stroke = Brushes.Black;
                    S.StrokeThickness = 2;
                    S.ClearValue(Path.StrokeDashArrayProperty);
                }
                try { ((Canvas)(C.Children[0])).Children.Remove(RemoveSeam); }
                catch { }
                if (C.Children.Count > 0)
                    ((Canvas)(C.Children[0])).Children.Add(S);
                else
                {
                    Canvas New = new Canvas();
                    New.Children.Add(S);
                    C.Children.Add(New);
                }
            }
        }
        private void GridMouseMove(object sender, MouseEventArgs e, StackPanel S, Grid G, Rectangle R,Canvas C)
        {
            StacksPanelMouseMove(sender, e, S, G, R);
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
            if ((Aim == "Seam" || Aim == "Spline") && SeamNodes.Count > 0)
            {
                if (Mouse.LeftButton == MouseButtonState.Pressed)
                {
                    PathMaker.Node A = SeamNodes[SeamNodes.Count - 1];
                    A.Nodetype = PathMaker.Node.NodeType.Symetrical;
                    A.ControlPoint2 = Mouse.GetPosition(C);

                    List<PathMaker.Node> NL = new List<PathMaker.Node>();
                    foreach (PathMaker.Node N in SeamNodes)
                    {
                        NL.Add(N);
                    }
                    NL.Add(new PathMaker.Node(NP: Mouse.GetPosition(C)));
                    RemoveSeam.Data = PathMaker.Node.GetPath(NL);
                    if (Aim == "Seam")
                    {
                        RemoveSeam.Stroke = Brushes.Red;
                        RemoveSeam.StrokeDashArray = new DoubleCollection() { 4, 2 };
                        RemoveSeam.StrokeThickness = 1;
                    }
                    else if (Aim == "Spline")
                    {
                        RemoveSeam.Stroke = Brushes.Black;
                        RemoveSeam.StrokeThickness = 2;
                        RemoveSeam.ClearValue(Path.StrokeDashArrayProperty);
                    }
                    try {
                        if (C.Children.Count > 0)
                            ((Canvas)(C.Children[0])).Children.Add(RemoveSeam);
                        else
                        {
                            Canvas New = new Canvas();
                            New.Children.Add(RemoveSeam);
                            C.Children.Add(New);
                        }
                        }
                    catch { }
                }
                else
                {
                    List<PathMaker.Node> NL = new List<PathMaker.Node>();
                    foreach (PathMaker.Node N in SeamNodes)
                    {
                        NL.Add(N);
                    }
                    NL.Add(new PathMaker.Node(NP: Mouse.GetPosition(C)));
                    RemoveSeam.Data = PathMaker.Node.GetPath(NL);
                    if (Aim == "Seam")
                    {
                        RemoveSeam.Stroke = Brushes.Red;
                        RemoveSeam.StrokeDashArray = new DoubleCollection() { 4, 2 };
                        RemoveSeam.StrokeThickness = 1;
                    }
                    else if (Aim == "Spline")
                    {
                        RemoveSeam.Stroke = Brushes.Black;
                        RemoveSeam.StrokeThickness = 2;
                        RemoveSeam.ClearValue(Path.StrokeDashArrayProperty);
                    }
                    try
                    {
                        if (C.Children.Count > 0)
                            ((Canvas)(C.Children[0])).Children.Add(RemoveSeam);
                        else
                        {
                            Canvas New = new Canvas();
                            New.Children.Add(RemoveSeam);
                            C.Children.Add(New);
                        }
                    }
                    catch { }
                }
            }
            else if (Aim == "Arrow" && FirstPoint != null)
            {
                    RemoveLineArrow.StartPoint = (Point)FirstPoint;
                    RemoveLineArrow.EndPoint = Mouse.GetPosition(C);
                    RemoveLineArrow.Caps = LineWithArrows.LineArrow.ArrowEnds.End;
                    RemoveLineArrow.Stroke = Brushes.Black;
                    try
                    {
                        if (C.Children.Count > 0)
                            ((Canvas)(C.Children[0])).Children.Add(RemoveLineArrow);
                        else
                        {
                            Canvas New = new Canvas();
                            New.Children.Add(RemoveLineArrow);
                            C.Children.Add(New);
                        }
                    }
                    catch { }

             }
             else if (Aim == "DoubleArrow" && FirstPoint != null)
             {
                    RemoveLineArrow.StartPoint = (Point)FirstPoint;
                    RemoveLineArrow.EndPoint = Mouse.GetPosition(C);
                    RemoveLineArrow.Caps = LineWithArrows.LineArrow.ArrowEnds.Both;
                    RemoveLineArrow.Stroke = Brushes.Black;
                    try
                    {
                        if (C.Children.Count > 0)
                            ((Canvas)(C.Children[0])).Children.Add(RemoveLineArrow);
                        else
                        {
                            Canvas New = new Canvas();
                            New.Children.Add(RemoveLineArrow);
                            C.Children.Add(New);
                        }
                    }
                    catch { }
              }
        }

                                 //FrontLeftMouseEvents//
        private void FrontLeftStackPanelMouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(sender, e, FrontLeftStackPanel, FrontLeftGrid, FrontLeftRectangle, FrontLeftCanvas);
        }
        private void FrontLeftStackPanelMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(FrontLeftStackPanel, e.Delta > 0, e.GetPosition(FrontLeftStackPanel));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void FrontLeftStackPanelMouseLeave(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow;
            MidPointPosition = null;
        }
        private void FrontLeftStackPanelMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            bool Ellipse;
            Ellipse=StacksPanelMouseLeftButtonsDown(sender, e,this.Part.FrontLeftPoints, FrontLeftStackPanel);
            if (!Ellipse)
            {
                GridMouseLeftButtonDown(FrontLeftStackPanel, FrontLeftCanvas);
            }
        }
        private void FrontLeftStackPanelMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(FrontLeftCanvas);
            SelectedPartEllipse = null;
        }

                                //FrontRightMouseEvents//
        private void FrontRightStackPanelMouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(sender, e, FrontRightStackPanel, FrontRightGrid, FrontRightRectangle, FrontRightCanvas);
        }
        private void FrontRightStackPanelMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(FrontRightStackPanel, e.Delta > 0, e.GetPosition(FrontRightStackPanel));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void FrontRightStackPanelMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            bool Ellipse;
            Ellipse=StacksPanelMouseLeftButtonsDown(sender, e, this.Part.FrontRightPoints, FrontRightStackPanel);
            if (!Ellipse)
            {
                GridMouseLeftButtonDown(FrontRightStackPanel, FrontRightCanvas);
            }
        }
        private void FrontRightGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(FrontRightCanvas);
            SelectedPartEllipse = null;
        }

                                //BackLeftMouseEvents//
        private void BackLeftStackPanelMouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(sender, e, BackLeftStackPanel, BackLeftGrid, BackLeftRectangle, BackLeftCanvas);
        }
        private void BackLeftStackPanelMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(BackLeftStackPanel, e.Delta > 0, e.GetPosition(BackLeftStackPanel));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void BackLeftStackPanelMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            bool Ellipse;
            Ellipse=StacksPanelMouseLeftButtonsDown(sender, e, Part.BackLeftPoints, BackLeftStackPanel);
            if (!Ellipse)
            {
                GridMouseLeftButtonDown(BackLeftStackPanel, BackLeftCanvas);
            }
        }
        private void BackLeftGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(BackLeftCanvas);
            SelectedPartEllipse = null;
        }

                                //BackRightMouseEvents//
        private void BackRightStackPanelMouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(sender, e, BackRightStackPanel, BackRightGrid, BackRightRectangle, BackRightCanvas);
        }
        private void BackRightStackPanelMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(BackRightStackPanel, e.Delta > 0, e.GetPosition(BackRightStackPanel));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void BackRightStackPanelMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            bool Ellipse;
            Ellipse=StacksPanelMouseLeftButtonsDown(sender, e, Part.BackRightPoints, BackRightStackPanel);
            if (!Ellipse)
            {
                GridMouseLeftButtonDown(BackRightStackPanel, BackRightCanvas);
            }
        }
        private void BackRightGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(BackRightCanvas);
            SelectedPartEllipse = null;
        }

        //Detay0MouseEvenets
        private void DetayGrid0_MouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(sender, e, DetayStackPanel0, DetayGrid0, DetayRectangle0, DetayCanvas0);
        }
        private void DetayGrid0_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(DetayStackPanel0, e.Delta > 0, e.GetPosition(DetayStackPanel0));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void DetayGrid0_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(DetayStackPanel0, DetayCanvas0);
        }
        private void DetayGrid0_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(DetayCanvas0); 
        }


        //Button Events
        private void DeleteClick(object sender, RoutedEventArgs e)
        {
            DeleteKey();
        }
        private void TakeClick(object sender, RoutedEventArgs e)
        {
            int i = 9;
            foreach (TabItem T in TABS.Items)
            {
                if (T.IsSelected)
                {
                    i = TABS.Items.IndexOf(T);
                    break;
                }
            }
            if (i == 0)
            {
                Canvas C = FrontLeftCanvas;
                TakePaths(C);
            }
            else if (i == 1)
            {
                Canvas C = FrontRightCanvas;
                TakePaths(C);
            }
            else if (i == 2)
            {
                Canvas C = BackLeftCanvas;
                TakePaths(C);
            }
            else if (i == 3)
            {
                Canvas C = BackRightCanvas;
                TakePaths(C);
            }
            else if (i == 4)
            {
                Canvas C = DetayCanvas0;
                TakePaths(C);
            }
            else if (i == 5)
            {
                Canvas C = DetayCanvas1;
                TakePaths(C);
            }
            else if (i == 6)
            {
                Canvas C = DetayCanvas2;
                TakePaths(C);
            }

        }
        private void CutClick(object sender, RoutedEventArgs e)
        {
            CutKey();
        }
        private void CopyClick(object sender, RoutedEventArgs e)
        {
            CopyKey();
        }
        private void PasteClick(object sender, RoutedEventArgs e)
        {
            PasteKey();
        }





        /******************/


        private void PartSaveClick(object sender, RoutedEventArgs e)
        {
            string noktaYok = " görünüş için referans noktaları belirsiz!";
            string gorunusYok = " görünüş boş!";

            if (PartName.Text == "")
            {
                MessageBox.Show("Lütfen parça ismini girin.");
                return;
            }

            if (SelectedType.FrontBack && SelectedType.LeftRight)
            {
                //FrontLeft
                if (FrontLeftCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Ön-Sol" + gorunusYok);
                    return;
                }
                else if (this.Part.FrontLeftPoints.Count < 2)
                {
                    MessageBox.Show("Ön-Sol" + noktaYok);
                    return;
                }

                //FrontRight
                if (FrontRightCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Ön-Sağ" + gorunusYok);
                    return;
                }
                else if (this.Part.FrontRightPoints.Count < 2)
                {
                    MessageBox.Show("Ön-Sağ" + noktaYok);
                    return;
                }

                //BackLeft
                if (BackLeftCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Arka-Sol" + gorunusYok);
                    return;
                }
                else if (this.Part.BackLeftPoints.Count < 2)
                {
                    MessageBox.Show("Arka-Sol" + noktaYok);
                    return;
                }

                //BackRight
                if (BackRightCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Arka-Sağ" + gorunusYok);
                    return;
                }
                else if (this.Part.BackRightPoints.Count < 2)
                {
                    MessageBox.Show("Arka-Sağ" + noktaYok);
                    return;
                }
            }

            else if (SelectedType.FrontBack && !SelectedType.LeftRight)
            {
                //Front
                if (FrontLeftCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Ön" + gorunusYok);
                    return;
                }
                else if (this.Part.FrontLeftPoints.Count < 2)
                {
                    MessageBox.Show("Ön Görünüş" + noktaYok);
                    return;
                }

                //Back
                if (BackLeftCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Arka" + gorunusYok);
                    return;
                }
                else if (this.Part.BackLeftPoints.Count < 2)
                {
                    MessageBox.Show("Arka Görünüş" + noktaYok);
                    return;
                }
            }
            else if (!SelectedType.FrontBack && SelectedType.LeftRight)
            {
                //FrontLeft
                if (FrontLeftCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Ön-Sol" + gorunusYok);
                    return;
                }
                else if (this.Part.FrontLeftPoints.Count < 2)
                {
                    MessageBox.Show("Ön-Sol" + gorunusYok);
                    return;
                }

                //FrontRight
                if (FrontRightCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Ön-Sağ" + gorunusYok);
                    return;
                }
                else if (this.Part.FrontRightPoints.Count < 2)
                {
                    MessageBox.Show("Ön-Sağ" + gorunusYok);
                    return;
                }
            }
            else if (!SelectedType.FrontBack && !SelectedType.LeftRight)
            {
                //Front
                if (FrontLeftCanvas.Children.Count < 1)
                {
                    MessageBox.Show("Ön" + gorunusYok);
                    return;
                }
                else if (this.Part.FrontLeftPoints.Count < 2)
                {
                    MessageBox.Show("Ön" + noktaYok);
                    return;
                }
            }



            //PreparePart To Save//
            this.Part.PartType = SelectedType;
            this.Part.TemplateName = SelectedTemplate.Name;
            this.Part.PartName = PartName.Text;
            this.Part.FrontLeftView = (Canvas)(FrontLeftCanvas.Children[0]);
            if (this.Part.PartType.FrontBack && this.Part.PartType.LeftRight)
            {
                this.Part.FrontRightView = (Canvas)(FrontRightCanvas.Children[0]);
                this.Part.BackLeftView = (Canvas)(BackLeftCanvas.Children[0]);
                this.Part.BackRightView = (Canvas)(BackRightCanvas.Children[0]);
            }
            if (this.Part.PartType.FrontBack && !this.Part.PartType.LeftRight)
            {
                this.Part.BackLeftView = (Canvas)(BackLeftCanvas.Children[0]);
            }
            if (!this.Part.PartType.FrontBack && this.Part.PartType.LeftRight)
            {
                this.Part.FrontRightView = (Canvas)(FrontRightCanvas.Children[0]);
            }

            this.Part.DetailViews.Clear();


            if (this.Part.PartType.PartType == "Yaka")
                AddDetail(DetayCanvas0, Detay0TableCheckBox, Detay0TableC);

            else if (this.Part.PartType.PartType == "Sev Böcek")
                AddDetail(DetayCanvas0, Detay0TableCheckBox, Detay0TableC);

            else if (this.Part.PartType.PartType == "Manşet")
                AddDetail(DetayCanvas0, Detay0TableCheckBox, Detay0TableC);

            else if (this.Part.PartType.PartType == "Pat")
            {
                AddDetail(DetayCanvas0, Detay0TableCheckBox, Detay0TableC);
                AddDetail(DetayCanvas1, Detay1TableCheckBox, Detay1TableC);
                AddDetail(DetayCanvas2, Detay2TableCheckBox, Detay2TableC);
            }

            this.Part.PartUpDate();

            //Save Part//
            Karbel.Data.SavePart(this.Part);

            MessageBox.Show("Parça kaydedildi!");
        }

        private void AddDetail(Canvas detay,CheckBox isTable,Canvas detayCanvas)
        {
            if (detay.Children.Count > 0)
                if (detay.Children[0] is Canvas)
                    if (((Canvas)(detay.Children[0])).Children.Count > 0)
                    {
                        pvdcd.DetailView DV = new pvdcd.DetailView();
                        DV.Detail = XO1.CanvasOperations.PreparedCanvasAll((Canvas)detay.Children[0]);
                        DV.IsTable = (bool)isTable.IsChecked;

                        isTable.IsChecked = !isTable.IsChecked;
                        isTable.IsChecked = !isTable.IsChecked;

                        if (detayCanvas.Children.Count > 0)
                        {
                            Table T = ((Table)(((FlowDocument)(((FlowDocumentReader)(Detay0TableC.Children[0])).Document)).Blocks.OfType<Table>().First<Table>()));
                            DV.DetailTable = T;
                        }
                        this.Part.DetailViews.Add(DV);
                    }
        }
        private void PartLoadClick(object sender, RoutedEventArgs e)
        {
            Karbel.Parcalar parcalar = new Karbel.Parcalar();
            parcalar.ShowDialog();

            if (parcalar.Parca == null)
                return;

            this.Part = parcalar.Parca;
              
            PartName.Text = this.Part.PartName;
            FillPartPoints();
            TemplateComboBox.Items.OfType<TextBlock>().ToList<TextBlock>().ForEach(T =>
            {
                if (T.Text == this.Part.TemplateName)
                {
                    TemplateComboBox.SelectedItem = T;
                }
            });
            PartTypeComboBox.Items.OfType<TextBlock>().ToList<TextBlock>().ForEach(T =>
            {
                if (T.Text == this.Part.PartType.PartType)
                {
                    PartTypeComboBox.SelectedItem = T;
                }
            });
            FrontLeftCanvas.Children.Clear();
            FrontLeftCanvas.Children.Add(this.Part.FrontLeftView);
            if (this.Part.PartType.FrontBack && this.Part.PartType.LeftRight)
            {
                FrontRightCanvas.Children.Clear();
                FrontRightCanvas.Children.Add(this.Part.FrontRightView);
                BackLeftCanvas.Children.Clear();
                BackLeftCanvas.Children.Add(this.Part.BackLeftView);
                BackRightCanvas.Children.Clear();
                BackRightCanvas.Children.Add(this.Part.BackRightView);
            }
            else if (this.Part.PartType.FrontBack && !this.Part.PartType.LeftRight)
            {
                BackLeftCanvas.Children.Clear();
                BackLeftCanvas.Children.Add(this.Part.BackLeftView);
            }
            else if (!this.Part.PartType.FrontBack && this.Part.PartType.LeftRight)
            {
                FrontRightCanvas.Children.Clear();
                FrontRightCanvas.Children.Add(this.Part.FrontRightView);
            }
            DetayCanvas0.Children.Clear();
            Detay0TableCheckBox.IsChecked = false;
            Detay0TableC.Children.Clear();
            if (this.Part.DetailViews.Count > 0)
            {
                DetayCanvas0.Children.Add(this.Part.DetailViews[0].Detail);
                Detay0TableCheckBox.IsChecked = this.Part.DetailViews[0].IsTable;
                Detay0TableC.Children.Clear();
                FlowDocument FD = pvdcd.DetailView.GetTable(this.Part.DetailViews[0].TableVeriables, this.Part.DetailViews[0].TableTitle);
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Document = FD;
                Detay0Number.Text = Convert.ToString(this.Part.DetailViews[0].TableVeriables.Count);
                Detay0TableC.Children.Add(FDR);
            }
            DetayCanvas1.Children.Clear();
            Detay1TableCheckBox.IsChecked = false;
            Detay1TableC.Children.Clear();
            if (this.Part.DetailViews.Count > 1)
            {
                DetayCanvas1.Children.Add(this.Part.DetailViews[1].Detail);
                Detay1TableCheckBox.IsChecked = this.Part.DetailViews[1].IsTable;
                Detay1TableC.Children.Clear();
                FlowDocument FD = pvdcd.DetailView.GetTable(this.Part.DetailViews[1].TableVeriables, this.Part.DetailViews[1].TableTitle);
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Document = FD;
                Detay1Number.Text = Convert.ToString(this.Part.DetailViews[1].TableVeriables.Count);
                Detay1TableC.Children.Add(FDR);
            }
            DetayCanvas2.Children.Clear();
            Detay2TableCheckBox.IsChecked = false;
            Detay2TableC.Children.Clear();
            if (this.Part.DetailViews.Count > 2)
            {
                DetayCanvas2.Children.Add(this.Part.DetailViews[2].Detail);
                Detay2TableCheckBox.IsChecked = this.Part.DetailViews[2].IsTable;
                Detay2TableC.Children.Clear();
                FlowDocument FD = pvdcd.DetailView.GetTable(this.Part.DetailViews[2].TableVeriables, this.Part.DetailViews[2].TableTitle);
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Document = FD;
                Detay2Number.Text = Convert.ToString(this.Part.DetailViews[2].TableVeriables.Count);
                Detay2TableC.Children.Add(FDR);
            }
        }
     
        //FrontLeftButtonEvents
        private void FrontLeftOpenClick(object sender, RoutedEventArgs e)
        {
            OpenSVG(FrontLeftCanvas);
        }

        private void OpenSVG(Canvas canvas)
        {
            System.IO.Stream MyStream = null;
            System.Windows.Forms.OpenFileDialog ofld = new System.Windows.Forms.OpenFileDialog();
            ofld.Filter = "svg files (*.svg)|*.svg";
            ofld.RestoreDirectory = true;
            if (ofld.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    if ((MyStream = ofld.OpenFile()) != null)
                    {
                        MyStream.Close();
                        String FileName = ofld.FileName;

                        Viewbox VB = SP1.Convertor.Svg2Viewbox(FileName);
                        if (VB != null)
                        {
                            Canvas C;
                            try
                            {
                                C = (Canvas)VB.Child;
                            }
                            catch { MessageBox.Show("Dosya açılamadı"); return; }

                            VB.Child = null;
                            canvas.Children.Clear();
                            canvas.Children.Add(C);
                        }
                        else
                            MessageBox.Show("Dosya açılamadı!"); return;

                    }
                }
                catch { MessageBox.Show("Dosya açılamadı"); return; }
            }
        }

        //FrontRightButtonEvents
        private void FrontRightOpenClick(object sender, RoutedEventArgs e)
        {
            OpenSVG(FrontRightCanvas);
        }

        //BackLeftButtonEvents
        private void BackLeftOpenClick(object sender, RoutedEventArgs e)
        {
            OpenSVG(BackLeftCanvas);
        }

        //BackRightButtonEvents 
        private void BackRightOpenClick(object sender, RoutedEventArgs e)
        {
            OpenSVG(BackRightCanvas);
        }
 
        //Detay0ButtonEvents
        private void Detay0OpenClick(object sender, RoutedEventArgs e)
        {
            OpenSVG(DetayCanvas0);
        } 

        //KeyEvents
        private void RemoveTrivialShapes()
        {
            try
            {
                ((Canvas)RemoveLineArrow.Parent).Children.Remove(RemoveLineArrow);
            }
            catch { }
            try
            {
                ((Canvas)RemoveSeam.Parent).Children.Remove(RemoveSeam);
            }
            catch { }
        }
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                EscapeKey();
            }
            else if (e.Key == Key.Delete)
            {
                DeleteKey();
            }
            else if (e.Key == Key.X && Keyboard.IsKeyDown(Key.LeftCtrl)) // for combination of Ctrl + X
            {
                CutKey();
            }
            else if (e.Key == Key.C && Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                CopyKey();
            }
            else if (e.Key == Key.V && Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                PasteKey();
            }
            else if (e.Key == Key.F1)
            {
                pvdcd.Project.OpenHelp();
            }
        }
        private void EscapeKey()
        {
            RemoveAdorners();
            SelectedUIElements.Clear();
            UnSelectTemplateEllipse();
            RemoveTrivialShapes();
            SeamNodes.Clear();
            Aim = "";
        }
        private void DeleteKey()
        {
            SelectedUIElements.ForEach(p =>
            {
                if(p is Path)
                ((Canvas)((Path)p).Parent).Children.Remove(p);
                else if(p is LineWithArrows.LineArrow)
                    ((Canvas)((LineWithArrows.LineArrow)p).Parent).Children.Remove(p);
                else if(p is TextBox)
                    ((Canvas)((TextBox)p).Parent).Children.Remove(p);
            });
            EscapeKey();
        }
        private void CutKey()
        {
            SelectedUIElements.ForEach(p =>
            {
                if (p is Path)
                    ((Canvas)((Path)p).Parent).Children.Remove(p);
                else if (p is LineWithArrows.LineArrow)
                    ((Canvas)((LineWithArrows.LineArrow)p).Parent).Children.Remove(p);
                else if (p is TextBox)
                    ((Canvas)((TextBox)p).Parent).Children.Remove(p);
            });
            CopiedUIElements.Clear();
            SelectedUIElements.ForEach(p =>
            {
                CopiedUIElements.Add((UIElement)XamlEditor.CloneUIElement.cloneElement(p));
            });
            EscapeKey();
        }
        private void CopyKey()
        {
            CopiedUIElements.Clear();
            SelectedUIElements.ForEach(p =>
            {
                CopiedUIElements.Add((UIElement)XamlEditor.CloneUIElement.cloneElement(p));
            });
            EscapeKey();
        }
        private void PasteKey()
        {
            int i=9;
            foreach (TabItem T in TABS.Items)
            {
                if (T.IsSelected)
                {
                    i=TABS.Items.IndexOf(T);
                    break;
                }
            }
            if (i == 0)
            { 
                Canvas C=FrontLeftCanvas;
                PastePaths(C);
            }
            else if (i == 1)
            { 
                Canvas C = FrontRightCanvas;
                PastePaths(C);
            }
            else if (i == 2)
            { 
                Canvas C = BackLeftCanvas;
                PastePaths(C);
            }
            else if (i == 3)
            {
                Canvas C = BackRightCanvas;
                PastePaths(C);
            }
            else if (i == 4)
            {
                Canvas C = DetayCanvas0;
                PastePaths(C);
            }
            else if (i == 5)
            {
                Canvas C = DetayCanvas1;
                PastePaths(C);
            }
            else if (i == 6)
            {
                Canvas C = DetayCanvas2;
                PastePaths(C);
            }
        }
        private void PastePaths(Canvas C)
        {
            if (C.Children.Count < 1)
            {
                Canvas NewCanvas = new Canvas();
                CopiedUIElements.ForEach(p => { NewCanvas.Children.Add(p); });
                C.Children.Add(NewCanvas);
                CopiedUIElements.Clear();
                EscapeKey();
                return;
            }
            else
            {
                if (C.Children[0] is Canvas)
                    C = (Canvas)(C.Children[0]);
                if (C.Children.Count > 0)
                {
                    if (C.Children[0] is Canvas)
                        C = (Canvas)(C.Children[0]);
                }
                if (C.Children.Count > 0)
                {
                    if (C.Children[0] is Canvas)
                        C = (Canvas)(C.Children[0]);
                }
                CopiedUIElements.ForEach(p => { C.Children.Add(p); });
                CopiedUIElements.Clear();
                EscapeKey();
                return;
            }
        }
        private void TakePaths(Canvas C)
        {
            if (SelectedUIElements.Count > 0)
            {
                C.Children.Clear();
                Canvas NewCanvas = new Canvas();
                SelectedUIElements.ForEach(p => { NewCanvas.Children.Add(XamlEditor.CloneUIElement.cloneElement(p)); });
                C.Children.Add(NewCanvas);
                SelectedUIElements.Clear();
                EscapeKey();
            }
        }

        //Table Events
        private List<pvdcd.TableProperty> GetTableProperty(Table T)
        {
            List<pvdcd.TableProperty> TPL = new List<pvdcd.TableProperty>();
            TableRowGroupCollection TRGC=T.RowGroups;
            if (!(TRGC.Count < 4))
            {
                for (int i = 3; i < TRGC.Count; ++i)
                {
                    
                    TableRowGroup TRG = TRGC[i];
                    TableRow TR = TRG.Rows[0];
                    pvdcd.TableProperty TP = new pvdcd.TableProperty();
                    int k = 0;
                    foreach (TableCell TC in TR.Cells)
                    {
                        if (k == 0)
                        {
                            TP.PropertyName=((TextBox)(((BlockUIContainer)(TC.Blocks.OfType<BlockUIContainer>().First<BlockUIContainer>())).Child)).Text;
                        }
                        else
                        {
                            TP.PropertyValues.Add(((TextBox)(((BlockUIContainer)(TC.Blocks.OfType<BlockUIContainer>().First<BlockUIContainer>())).Child)).Text);
                        }
                        ++k;
                    }
                    TPL.Add(TP);
                }
            }
            return TPL;
        }
                            //Detay0 Events
        private void Detay0TableChecked(object sender, RoutedEventArgs e)
        {
            Detay0TableCanvas.Visibility = Visibility.Visible;
            if (Detay0TableC.Children.Count < 1)
            {
                List<pvdcd.TableProperty> a = new List<pvdcd.TableProperty>();
                a.Add(new pvdcd.TableProperty());
                FlowDocument FD = pvdcd.DetailView.GetTable(a,"");
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Width = 999999;
                FDR.ViewingMode = FlowDocumentReaderViewingMode.Page;
                FDR.IsFindEnabled = false;
                FDR.IsScrollViewEnabled = false;
                FDR.IsPrintEnabled = false;
                FDR.Document = FD;
                Detay0TableC.Children.Add(FDR); 
            }
        }
        private void Detay0TableUnchecked(object sender, RoutedEventArgs e)
        {
            Detay0TableCanvas.Visibility = Visibility.Collapsed;
        }
        private void Detay0DecreaseClick(object sender, RoutedEventArgs e)
        {
            int i = Convert.ToInt32(Detay0Number.Text);
            --i;
            if (i < 1)
            {
                i = 1;
                Detay0Number.Text = Convert.ToString(i);
                return;
            }
            else
            {
                Table T = ((Table)(((FlowDocument)(((FlowDocumentReader)(Detay0TableC.Children[0])).Document)).Blocks.OfType<Table>().First<Table>()));
                List<pvdcd.TableProperty> TPL = GetTableProperty(T);
                TPL.RemoveAt(TPL.Count - 1);
                Detay0TableC.Children.Clear();
                FlowDocument FD = pvdcd.DetailView.GetTable(TPL,((TextBox)(((BlockUIContainer)(T.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock)).Child)).Text );
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Width = 999999;
                FDR.Document = FD;
                Detay0TableC.Children.Add(FDR);
                Detay0Number.Text = Convert.ToString(i);
            }
        }
        private void Detay0IncreaseClick(object sender, RoutedEventArgs e)
        {
            int i = Convert.ToInt32(Detay0Number.Text);
            ++i;
            Detay0Number.Text = Convert.ToString(i);
            Table T = ((Table)(((FlowDocument)(((FlowDocumentReader)(Detay0TableC.Children[0])).Document)).Blocks.OfType<Table>().First<Table>()));
            List<pvdcd.TableProperty> TPL=GetTableProperty(T);
            TPL.Add(new pvdcd.TableProperty());
            Detay0TableC.Children.Clear();
            FlowDocument FD = pvdcd.DetailView.GetTable(TPL, ((TextBox)(((BlockUIContainer)(T.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock)).Child)).Text);
            FlowDocumentReader FDR = new FlowDocumentReader();
            FDR.Width = 999999;
            FDR.Document = FD;
            Detay0TableC.Children.Add(FDR);
        }

        private void AddNoteClick(object sender, RoutedEventArgs e)
        {
            Aim = "Note";
        }
        private void AddArrowClick(object sender, RoutedEventArgs e)
        {
            Aim = "Arrow";
        }
        private void AddDoubleArrowClick(object sender, RoutedEventArgs e)
        {
            Aim = "DoubleArrow";
        }
        private void AddSplineClick(object sender, RoutedEventArgs e)
        {
            Aim = "Spline";
        }
        private void AddSeamClick(object sender, RoutedEventArgs e)
        {
            Aim = "Seam";
        }

        #region Detay1
        private void DetayGrid1_MouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(sender, e, DetayStackPanel1, DetayGrid1, DetayRectangle1, DetayCanvas1);
        }
        private void DetayGrid1_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(DetayStackPanel1, e.Delta > 0, e.GetPosition(DetayStackPanel1));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void DetayGrid1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(DetayStackPanel1, DetayCanvas1);
        }
        private void DetayGrid1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(DetayCanvas1); 
        }
        private void Detay1OpenClick(object sender, RoutedEventArgs e)
        {
            OpenSVG(DetayCanvas1);
        }
        private void Detay1TableChecked(object sender, RoutedEventArgs e)
        {
            Detay1TableCanvas.Visibility = Visibility.Visible;
            if (Detay1TableC.Children.Count < 1)
            {
                List<pvdcd.TableProperty> a = new List<pvdcd.TableProperty>();
                a.Add(new pvdcd.TableProperty());
                FlowDocument FD = pvdcd.DetailView.GetTable(a, "");
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Width = 999999;
                FDR.ViewingMode = FlowDocumentReaderViewingMode.Page;
                FDR.IsFindEnabled = false;
                FDR.IsScrollViewEnabled = false;
                FDR.IsPrintEnabled = false;
                FDR.Document = FD;
                Detay1TableC.Children.Add(FDR);
            }
        }
        private void Detay1TableUnchecked(object sender, RoutedEventArgs e)
        {
            Detay1TableCanvas.Visibility = Visibility.Collapsed;
        }
        private void Detay1DecreaseClick(object sender, RoutedEventArgs e)
        {
            int i = Convert.ToInt32(Detay1Number.Text);
            --i;
            if (i < 1)
            {
                i = 1;
                Detay1Number.Text = Convert.ToString(i);
                return;
            }
            else
            {
                Table T = ((Table)(((FlowDocument)(((FlowDocumentReader)(Detay1TableC.Children[0])).Document)).Blocks.OfType<Table>().First<Table>()));
                List<pvdcd.TableProperty> TPL = GetTableProperty(T);
                TPL.RemoveAt(TPL.Count - 1);
                Detay1TableC.Children.Clear();
                FlowDocument FD = pvdcd.DetailView.GetTable(TPL, ((TextBox)(((BlockUIContainer)(T.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock)).Child)).Text);
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Width = 999999;
                FDR.Document = FD;
                Detay1TableC.Children.Add(FDR);
                Detay1Number.Text = Convert.ToString(i);
            }
        }
        private void Detay1IncreaseClick(object sender, RoutedEventArgs e)
        {
            int i = Convert.ToInt32(Detay1Number.Text);
            ++i;
            Detay1Number.Text = Convert.ToString(i);
            Table T = ((Table)(((FlowDocument)(((FlowDocumentReader)(Detay1TableC.Children[0])).Document)).Blocks.OfType<Table>().First<Table>()));
            List<pvdcd.TableProperty> TPL = GetTableProperty(T);
            TPL.Add(new pvdcd.TableProperty());
            Detay1TableC.Children.Clear();
            FlowDocument FD = pvdcd.DetailView.GetTable(TPL, ((TextBox)(((BlockUIContainer)(T.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock)).Child)).Text);
            FlowDocumentReader FDR = new FlowDocumentReader();
            FDR.Width = 999999;
            FDR.Document = FD;
            Detay1TableC.Children.Add(FDR);
        }
        #endregion Detay1

        #region Detay2
        private void DetayGrid2_MouseMove(object sender, MouseEventArgs e)
        {
            GridMouseMove(sender, e, DetayStackPanel2, DetayGrid2, DetayRectangle2, DetayCanvas2);
        }
        private void DetayGrid2_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(DetayStackPanel2, e.Delta > 0, e.GetPosition(DetayStackPanel2));
            try
            {
                ADL.InvalidateVisual();
            }
            catch { }
        }
        private void DetayGrid2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonDown(DetayStackPanel2, DetayCanvas2);
        }
        private void DetayGrid2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            GridMouseLeftButtonUp(DetayCanvas2);
        }
        private void Detay2OpenClick(object sender, RoutedEventArgs e)
        {
            OpenSVG(DetayCanvas2);
        }
        private void Detay2TableChecked(object sender, RoutedEventArgs e)
        {
            Detay2TableCanvas.Visibility = Visibility.Visible;
            if (Detay2TableC.Children.Count < 1)
            {
                List<pvdcd.TableProperty> a = new List<pvdcd.TableProperty>();
                a.Add(new pvdcd.TableProperty());
                FlowDocument FD = pvdcd.DetailView.GetTable(a, "");
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Width = 999999;
                FDR.ViewingMode = FlowDocumentReaderViewingMode.Page;
                FDR.IsFindEnabled = false;
                FDR.IsScrollViewEnabled = false;
                FDR.IsPrintEnabled = false;
                FDR.Document = FD;
                Detay2TableC.Children.Add(FDR);
            }
        }
        private void Detay2TableUnchecked(object sender, RoutedEventArgs e)
        {
            Detay2TableCanvas.Visibility = Visibility.Collapsed;
        }
        private void Detay2DecreaseClick(object sender, RoutedEventArgs e)
        {
            int i = Convert.ToInt32(Detay2Number.Text);
            --i;
            if (i < 1)
            {
                i = 1;
                Detay2Number.Text = Convert.ToString(i);
                return;
            }
            else
            {
                Table T = ((Table)(((FlowDocument)(((FlowDocumentReader)(Detay2TableC.Children[0])).Document)).Blocks.OfType<Table>().First<Table>()));
                List<pvdcd.TableProperty> TPL = GetTableProperty(T);
                TPL.RemoveAt(TPL.Count - 1);
                Detay2TableC.Children.Clear();
                FlowDocument FD = pvdcd.DetailView.GetTable(TPL, ((TextBox)(((BlockUIContainer)(T.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock)).Child)).Text);
                FlowDocumentReader FDR = new FlowDocumentReader();
                FDR.Width = 999999;
                FDR.Document = FD;
                Detay2TableC.Children.Add(FDR);
                Detay2Number.Text = Convert.ToString(i);
            }
        }
        private void Detay2IncreaseClick(object sender, RoutedEventArgs e)
        {
            int i = Convert.ToInt32(Detay2Number.Text);
            ++i;
            Detay2Number.Text = Convert.ToString(i);
            Table T = ((Table)(((FlowDocument)(((FlowDocumentReader)(Detay2TableC.Children[0])).Document)).Blocks.OfType<Table>().First<Table>()));
            List<pvdcd.TableProperty> TPL = GetTableProperty(T);
            TPL.Add(new pvdcd.TableProperty());
            Detay2TableC.Children.Clear();
            FlowDocument FD = pvdcd.DetailView.GetTable(TPL, ((TextBox)(((BlockUIContainer)(T.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock)).Child)).Text);
            FlowDocumentReader FDR = new FlowDocumentReader();
            FDR.Width = 999999;
            FDR.Document = FD;
            Detay2TableC.Children.Add(FDR);
        }
        #endregion Detay2
    }
}
